package com.elinkway.infinitemovies.adapter;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.elinkway.infinitemovies.utils.LogUtils;

import java.util.ArrayList;

/**
 * Created by zhaopeipei on 2016/5/19.
 */
public class SplashViewPagerAdapter extends PagerAdapter {

    private final static String TAG = "SplashViewPagerAdapter";

    private ArrayList<ImageView> mImageViews;

    public SplashViewPagerAdapter(ArrayList<ImageView> views) {
        mImageViews = views;
    }
    @Override
    public int getCount() {
        if (mImageViews == null || mImageViews.size() <= 0) {
            return 3;
        } else {
            return mImageViews.size();
        }
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view == object);
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
        //((ViewPager) container).removeView(mImageViews.get(position));
    }

    // 每次滑动的时候生成的组件
    @Override
    public Object instantiateItem(View container, int position) {
        try {
            LogUtils.e(TAG, "!!!!!instantiateItem!!!!!" + position % mImageViews.size());
            ((ViewPager) container).removeView(mImageViews.get(position % mImageViews.size()));
            ((ViewPager) container).addView(mImageViews.get(position % mImageViews.size()), 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mImageViews.get(position % mImageViews.size());
    }

}
